package com.yanxin

import java.io.{ObjectInputStream, ObjectOutputStream}
import java.net.Socket

class RPCClient {
  //返回值需要一个Socket
  def connect(host:String,port:Int):Socket = {
    //RPC之间传递的内容是case class 或者case object

    new Socket(host,port)
  }
}

object RPCClient{
  def main(args: Array[String]): Unit = {

    val client = new RPCClient
    val socket: Socket = client.connect("127.0.0.1",8888)

    //通信(获取输出流)
    val oos: ObjectOutputStream = new ObjectOutputStream(socket.getOutputStream)

    //定义通信的消息
    oos.writeObject(RegisterMsg("tom","123"))
    oos.flush()

    //获得响应
    //从输入流中获取响应
    val ois: ObjectInputStream = new ObjectInputStream(socket.getInputStream) //ois是接收服务端返回来的消息
    val response: AnyRef = ois.readObject()
    //模式匹配
    response match {
      case ResultMsg(result) =>{
        println("a msg from server: "+result)
      }


    }

    oos.close()
    socket.close()
  }
}
